package server

import (
	"errors"
	"zk1/common/models"
	"zk1/common/pkg"
	"zk1/common/proto/user"
)

func SendSms(in *user.SendSmsRequest) (*user.SendSmsResponse, error) {
	u := models.User{
		Mobile: in.Mobile,
		Code:   in.Code,
	}
	err := u.SendSms()
	if err != nil {
		return nil, err
	}
	err = pkg.SendSms(in.Mobile, in.Code)
	if err != nil {
		return nil, errors.New("手机号发送验证码失败")
	}
	return &user.SendSmsResponse{Status: int64(u.ID)}, nil
}

func RegisterLogin(in *user.RegisterLoginRequest) (*user.RegisterLoginResponse, error) {
	login, err := models.RegisterLogin(in.Mobile, in.Code)
	if err != nil {
		return nil, err
	}
	return &user.RegisterLoginResponse{UserId: int64(login.ID)}, nil
}

func HomePage(in *user.HomePageRequest) (*user.HomePageResponse, error) {
	page, err := models.HomePage(in.Mobile, in.Code)
	if err != nil {
		return nil, err
	}
	return &user.HomePageResponse{
		UserId:   int64(page.ID),
		Picture:  page.Picture,
		NickName: page.NickName,
	}, nil
}
